﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using AutoMapper;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using Sautom.ClientComunication;
using Sautom.ClientComunication.CommandService;
using Sautom.ClientComunication.FileService;
using Sautom.ClientComunication.Helpers;
using Sautom.ClientComunication.QueriesService;
using Sautom.ClientComunication.Services;
using Sautom.Modules.Client.Controls.ViewModels;
using Sautom.Modules.Client.Models;

namespace Sautom.Modules.Client.ViewModels 
{
    public sealed class ContractViewModel : NavigationAwareNotificationObject
    {
        #region Constructor
        public ContractViewModel(IRegionManager regionManager, ServiceFactory serviceFactory)
        {
            RegionManager = regionManager;

            QueriesServiceClient = serviceFactory.GetQueriesService();
            FileServiceClient = serviceFactory.GetFilesService(); 

            ClientFileHelper = new ClientFileHelper(null);

            BackCommand = new DelegateCommand(ExecuteBackCommand);
            EditCommand = new DelegateCommand(ExecuteEditCommand);

            PrintConsultingCommand = new DelegateCommand<string>(ExecutePrintConsultingCommand, (str) => Id != Guid.Empty);
        }

        #endregion

        #region Commands
        public DelegateCommand EditCommand { get; private set; }
        public DelegateCommand BackCommand { get; private set; }
        public DelegateCommand<string> PrintConsultingCommand { get; private set; }

        private void ExecutePrintConsultingCommand(string type)
        {
            if (!string.IsNullOrEmpty(type) && Id != Guid.Empty)
            {
                var fileData = FileServiceClient.ClientContract(Id, type);

                if (fileData != null && fileData.FileData.Length != 0)
                {
                    ClientFileHelper.PrintDocxFile(fileData.FileData);
                }
            }
        }

        private void ExecuteEditCommand()
        {
            var uri = new Uri(PathProvider.ContractEdit +
                new UriQuery { 
                { "contractId", Id.ToString() }, 
                { "clientId", ClientId.ToString() }, 
                { "orderId", OrderId.ToString() } },
                UriKind.Relative);
            RegionManager.RequestNavigate(RegionProvider.MainRegion, uri);
        }

        private void ExecuteBackCommand()
        {
            var uri = new Uri(PathProvider.ClientDetails +
                new UriQuery
                    {
                        {
                            "clientId",
                            ClientId.ToString()
                            }
                    },
                UriKind.Relative);
            RegionManager.RequestNavigate(RegionProvider.MainRegion, uri);
        }
        #endregion

        #region Properties
        private IRegionManager RegionManager { get; set; }
        private IQueriesService QueriesServiceClient { get; set; }
        private IFileService FileServiceClient { get; set; }
        
        private ClientFileHelper ClientFileHelper { get; set; }
        #endregion

        #region ViewModel Properties
        public Guid ClientId { get; set; }

        public Guid Id { get; set; }

        //consulting section
        private string _consultingNumber;
        public string ConsultingNumber
        {
            get { return _consultingNumber; }
            set
            {
                _consultingNumber = value;
                RaisePropertyChanged(() => ConsultingNumber);
            }
        }

        private DateTime? _consultingDate;
        public DateTime? ConsultingDate
        {
            get { return _consultingDate; }
            set
            {
                _consultingDate = value;
                RaisePropertyChanged(() => ConsultingDate);
            }
        }

        private float _consultingHours;
        public float ConsultingHours
        {
            get { return _consultingHours; }
            set
            {
                _consultingHours = value;
                RaisePropertyChanged(() => ConsultingHours);
            }
        }

        private float _consultingSum;
        public float ConsultingSum
        {
            get { return _consultingSum; }
            set
            {
                _consultingSum = value;
                RaisePropertyChanged(() => ConsultingSum);
            }
        }

        private DateTime? _consultingActDate;
        public DateTime? ConsultingActDate
        {
            get { return _consultingActDate; }
            set
            {
                _consultingActDate = value;
                RaisePropertyChanged(() => ConsultingActDate);
            }
        }

        public RateItemDtoOutput ConsultingRate { get; set; }

        //workshop section
        private string _workshopNumber;
        public string WorkshopNumber
        {
            get { return _workshopNumber; }
            set
            {
                _workshopNumber = value;
                RaisePropertyChanged(() => WorkshopNumber);
            }
        }

        private DateTime? _workshopDate;
        public DateTime? WorkshopDate
        {
            get { return _workshopDate; }
            set
            {
                _workshopDate = value;
                RaisePropertyChanged(() => WorkshopDate);
            }
        }

        private float _workshopHours;
        public float WorkshopHours
        {
            get { return _workshopHours; }
            set
            {
                _workshopHours = value;
                RaisePropertyChanged(() => WorkshopHours);
            }
        }

        private float _workshopSum;
        public float WorkshopSum
        {
            get { return _workshopSum; }
            set
            {
                _workshopSum = value;
                RaisePropertyChanged(() => WorkshopSum);
            }
        }

        private DateTime? _workshopActDate;
        public DateTime? WorkshopActDate
        {
            get { return _workshopActDate; }
            set
            {
                _workshopActDate = value;
                RaisePropertyChanged(() => InvoiceDate);
            }
        }

        public RateItemDtoOutput WorkshopRate { get; set; }

        //invoice section
        private DateTime? _invoiceDate;
        public DateTime? InvoiceDate
        {
            get { return _invoiceDate; }
            set
            {
                _invoiceDate = value;
                RaisePropertyChanged(() => InvoiceDate);
            }
        }

        private int _invoiceSum;
        public int InvoiceSum
        {
            get { return _invoiceSum; }
            set
            {
                _invoiceSum = value;
                RaisePropertyChanged(() => InvoiceSum);
            }
        }

        public bool IsShowContract { get { return Id != default(Guid); } }

        public Guid OrderId { get; set; }
        #endregion

        #region INavigationAware

        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            base.OnNavigatedTo(navigationContext);

            string orderId = navigationContext.Parameters["orderId"];
            string clientId = navigationContext.Parameters["clientId"];
            if (String.IsNullOrWhiteSpace(orderId) || String.IsNullOrWhiteSpace(clientId)) return;

            OrderId = Guid.Parse(orderId);
            ClientId = Guid.Parse(clientId);

            var contract = QueriesServiceClient.ContractForView(OrderId);

            Mapper.CreateMap<ContractViewDtoOutput, ContractViewModel>();
            Mapper.Map(contract, this);
        }

        #endregion
    }
}
